namespace Common {
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Reflection;
    using System.Text;
    using System.Text.RegularExpressions;

    public class JsonHelper {
        private static readonly string _COMMA = "@comma";
        private static JSONObject _json = new JSONObject();
        private static readonly string _SEMICOLON = "@semicolon";

        public static string DataTableToJson(string jsonName, DataTable dataTable_0) {
            StringBuilder builder = new StringBuilder();
            builder.Append("[");
            if (dataTable_0.Rows.Count > 0) {
                for (int i = 0; i < dataTable_0.Rows.Count; i++) {
                    builder.Append("{");
                    for (int j = 0; j < dataTable_0.Columns.Count; j++) {
                        builder.Append("\"" + dataTable_0.Columns[j].ColumnName.ToString() + "\":\"" + dataTable_0.Rows[i][j].ToString() + "\"");
                        if (j < (dataTable_0.Columns.Count - 1)) {
                            builder.Append(",");
                        }
                    }
                    builder.Append("}");
                    if (i < (dataTable_0.Rows.Count - 1)) {
                        builder.Append(",");
                    }
                }
            }
            builder.Append("]");
            return builder.ToString();
        }

        private static string Deserialize(string text) {
            text = StrEncode(text);
            int num2 = 0;
            string str = string.Empty;
            string pattern = @"(\{[^\[\]\{\}]+\})|(\[[^\[\]\{\}]+\])";
            while (Regex.IsMatch(text, pattern)) {
                foreach (Match match in Regex.Matches(text, pattern)) {
                    str = "___key" + num2 + "___";
                    if (match.Value.Substring(0, 1) == "{") {
                        _json.Add(str, DeserializeSingletonObject(match.Value));
                    }
                    else {
                        _json.Add(str, DeserializeSingletonArray(match.Value));
                    }
                    text = text.Replace(match.Value, str);
                    num2++;
                }
            }
            return text;
        }

        public static JSONArray DeserializeArray(string text) {
            _json = new JSONObject();
            return (_json[Deserialize(text)] as JSONArray);
        }

        public static JSONObject DeserializeObject(string text) {
            _json = new JSONObject();
            return (_json[Deserialize(text)] as JSONObject);
        }

        private static JSONArray DeserializeSingletonArray(string text) {
            JSONArray array = new JSONArray();
            foreach (Match match in Regex.Matches(text, "(\\\"(?<value>[^,\\\"]+)\")|(?<value>[^,\\[\\]]+)")) {
                string key = match.Groups["value"].Value;
                array.Add(_json.ContainsKey(key) ? _json[key] : StrDecode(key));
            }
            return array;
        }

        private static JSONObject DeserializeSingletonObject(string text) {
            JSONObject obj2 = new JSONObject();
            foreach (Match match in Regex.Matches(text, "(\\\"(?<key>[^\\\"]+)\\\":\\\"(?<value>[^,\\\"]+)\\\")|(\\\"(?<key>[^\\\"]+)\\\":(?<value>[^,\\\"\\}]+))")) {
                string key = match.Groups["value"].Value;
                obj2.Add(match.Groups["key"].Value, _json.ContainsKey(key) ? _json[key] : StrDecode(key));
            }
            return obj2;
        }

        public static T FromJson<T>(string strJson) where T : class {
            if (!string.IsNullOrEmpty(strJson)) {
                return JsonConvert.DeserializeObject<T>(strJson);
            }
            return default(T);
        }

        public static string ObjectToJson<T>(string jsonName, IList<T> IL) {
            StringBuilder builder = new StringBuilder();
            builder.Append("[");
            if (IL.Count > 0) {
                for (int i = 0; i < IL.Count; i++) {
                    PropertyInfo[] properties = Activator.CreateInstance<T>().GetType().GetProperties();
                    builder.Append("{");
                    for (int j = 0; j < properties.Length; j++) {
                        builder.Append(string.Concat(new object[] { "\"", properties[j].Name.ToString(), "\":\"", properties[j].GetValue(IL[i], null), "\"" }));
                        if (j < (properties.Length - 1)) {
                            builder.Append(",");
                        }
                    }
                    builder.Append("}");
                    if (i < (IL.Count - 1)) {
                        builder.Append(",");
                    }
                }
            }
            builder.Append("]");
            return builder.ToString();
        }

        public static string SerializeArray(JSONArray jsonArray) {
            StringBuilder builder = new StringBuilder();
            builder.Append("[");
            for (int i = 0; i < jsonArray.Count; i++) {
                if (jsonArray[i] is JSONObject) {
                    builder.Append(string.Format("{0},", SerializeObject((JSONObject)jsonArray[i])));
                }
                else if (jsonArray[i] is JSONArray) {
                    builder.Append(string.Format("{0},", SerializeArray((JSONArray)jsonArray[i])));
                }
                else if (jsonArray[i] is string) {
                    builder.Append(string.Format("\"{0}\",", jsonArray[i]));
                }
                else {
                    builder.Append(string.Format("\"{0}\",", ""));
                }
            }
            if (builder.Length > 1) {
                builder.Remove(builder.Length - 1, 1);
            }
            builder.Append("]");
            return builder.ToString();
        }

        public static string SerializeObject(JSONObject jsonObject) {
            StringBuilder builder = new StringBuilder();
            builder.Append("{");
            foreach (KeyValuePair<string, object> pair in jsonObject) {
                if (pair.Value is JSONObject) {
                    builder.Append(string.Format("\"{0}\":{1},", pair.Key, SerializeObject((JSONObject)pair.Value)));
                }
                else {
                    if (pair.Value is JSONArray) {
                        builder.Append(string.Format("\"{0}\":{1},", pair.Key, SerializeArray((JSONArray)pair.Value)));
                        continue;
                    }
                    if (pair.Value is string) {
                        builder.Append(string.Format("\"{0}\":\"{1}\",", pair.Key, pair.Value));
                        continue;
                    }
                    builder.Append(string.Format("\"{0}\":\"{1}\",", pair.Key, ""));
                }
            }
            if (builder.Length > 1) {
                builder.Remove(builder.Length - 1, 1);
            }
            builder.Append("}");
            return builder.ToString();
        }

        private static string StrDecode(string text) {
            return text.Replace(_SEMICOLON, ":").Replace(_COMMA, ",");
        }

        private static string StrEncode(string text) {
            foreach (Match match in Regex.Matches(text, "\\\"[^\\\"]+\\\"")) {
                text = text.Replace(match.Value, match.Value.Replace(":", _SEMICOLON).Replace(",", _COMMA));
            }
            return text;
        }

        public static string ToJson(object object_0) {
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.NullValueHandling = NullValueHandling.Include;
            return JsonConvert.SerializeObject(object_0, Formatting.Indented, settings);
        }

        public static string ToJson(object object_0, bool HasNullIgnore) {
            if (HasNullIgnore) {
                JsonSerializerSettings settings = new JsonSerializerSettings();
                settings.NullValueHandling = NullValueHandling.Ignore;
                return JsonConvert.SerializeObject(object_0, Formatting.Indented, settings);
            }
            return ToJson(object_0);
        }
    }
}

